package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.PersonAttendanceDao;
import cn.topevery.garden.entity.po.PersonAttendance;
import cn.topevery.garden.entity.query.PersonAttendanceQuery;
import cn.topevery.garden.entity.vo.CoveragePercentVO;
import cn.topevery.garden.entity.vo.GreLandVO;
import cn.topevery.garden.entity.vo.PersonAttendanceVO;

import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.DataFillHelper;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.DataReader;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.Value;
import hw.topevery.framework.entity.KeyValue;
import hw.topevery.framework.enums.DbTypeEnum;
import hw.topevery.framework.util.LogUtil;
import hw.topevery.framework.db.DbScriptHelper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 考勤记录
 */
@Repository
public class PersonAttendanceDaoImpl extends BaseDaoImpl<PersonAttendance> implements PersonAttendanceDao {
    @Override
    public <P extends DbCsp> List<PersonAttendance> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<PersonAttendanceVO> listPage(PersonAttendanceQuery query) {
        String table = "t_person_attendance tpa";

        String columns = "tpa.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tpa.c_id desc");
        sqlQueryMate
                .where(true, "tpa.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, PersonAttendanceVO.class);
    }

    @Override
    public PersonAttendance getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public DataPager<PersonAttendanceVO> personAttendanceStat(PersonAttendanceQuery query) {
        String table = "t_person_attendance tpa";

        String columns = "c_user_id,c_user_name,sum(c_sign_in_overtimes) c_sign_in_overtimes ,sum(c_sign_out_overtimes) c_sign_out_overtimes, sum(c_is_empty*8*60) c_is_empty,sum(c_is_overtimes) c_is_overtimes,sum( TIMESTAMPDIFF(MINUTE ,c_sign_in,c_sign_out)) c_out_range";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tpa.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.beginDate),"tpa.c_date",ScriptConditionEnum.GreaterThanEq,query.beginDate)
                .where(StringUtils.isNotBlank(query.endDate),"tpa.c_date",ScriptConditionEnum.LessThanEq,query.endDate)
        ;
        sqlQueryMate.setGroupBy("c_user_id,c_user_name");
        return getDataPager(sqlQueryMate, pageSize, pageIndex, PersonAttendanceVO.class);
    }
}
